package org.btik.light.tool;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class AESUtil {

    public static final String ECB_PKCS5Padding = "AES/ECB/PKCS5Padding";

    /**
     * 大概是这个默认大概是 AES/ECB/PKCS5Padding
     */
    private static final String ALGORITHM = "AES";

    /**
     *不支持加盐
     */
    public static byte[] encrypt(String transformation, byte[] planText, byte[] key) throws InvalidKeyException {
        Cipher cipher;

        try {
            cipher = Cipher.getInstance(transformation);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            e.printStackTrace();
            return null;
        }
        SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        cipher.update(planText);
        try {
            return cipher.doFinal();
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 使用默认模式,大概是 AES/ECB/PKCS5Padding
     */
    public static byte[] encrypt(byte[] planText, byte[] key) throws InvalidKeyException {
        return encrypt(ALGORITHM, planText, key);
    }

    /**
     * 使用默认模式,大概是 AES/ECB/PKCS5Padding
     */
    public static byte[] encryptStr(String planText, byte[] key) throws InvalidKeyException {
        return encrypt(ALGORITHM, planText.getBytes(StandardCharsets.UTF_8), key);
    }


    /**
     * 使用默认模式,大概是 AES/ECB/PKCS5Padding
     */
    public static byte[] encryptBase64Str(String planTextBase64Str, byte[] key) throws InvalidKeyException {
        return encrypt(ALGORITHM, Base64.getDecoder().decode(planTextBase64Str), key);
    }

    /**
     * 使用默认模式,大概是 AES/ECB/PKCS5Padding
     */
    public static byte[] decrypt(byte[] ciphertext, byte[] key) throws InvalidKeyException {
        return decrypt(ALGORITHM, ciphertext, key);
    }


    /**
     * 使用默认模式,大概是 AES/ECB/PKCS5Padding
     */
    public static byte[] decryptBase64(String transformation, String ciphertext, byte[] key) throws InvalidKeyException {
        return decrypt(transformation, Base64.getDecoder().decode(ciphertext), key);
    }
    /**
     *不支持加盐
     */
    public static byte[] decrypt(String transformation, byte[] ciphertext, byte[] key) throws InvalidKeyException {
        Cipher cipher;

        try {
            cipher = Cipher.getInstance(transformation);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            e.printStackTrace();
            return null;
        }
        SecretKeySpec keySpec = new SecretKeySpec(key, ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        cipher.update(ciphertext);
        try {
            return cipher.doFinal();
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
            return null;
        }
    }



}
